package com.mzy.linear_struct;

/**
 * @Author: codermzy
 * @Date: 2024/03/29/13:50
 * @Description:
 */
public class MyQueue<T> implements IQueue<T> {

    private final T[] data;

    private int size;


    private int front;

    public MyQueue(int capacity) {
        data = (T[]) new Object[capacity];
        size = 0;
    }

    @Override
    public void enqueue(T elem) {
        if (size == data.length) {
            throw new RuntimeException("当前队列已满");
        }
        int tail = (front + size) % capacity();
        data[tail] = elem;
        size++;
    }

    @Override
    public T dequeue() {
        if (isEmpty()) {
            throw new RuntimeException("当前队列为空");
        }
        T elem = data[front];
        front = (front + 1) % capacity();
        size--;
        return elem;
    }

    @Override
    public T peek() {
        return data[front];
    }

    @Override
    public int size() {
        return size;
    }

    public int capacity() {
        return data.length;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }


    public static void main(String[] args) {
        MyQueue<Integer> queue = new MyQueue<>(3);
        queue.enqueue(1);
        queue.enqueue(2);
        queue.enqueue(3);
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        queue.enqueue(4);
        queue.enqueue(5);
        queue.enqueue(6);
    }
}
